package avicit.bdp.dcs.desensitization.rest;

import avicit.bdp.dcs.desensitization.dto.DataMaskingMethodDto;
import avicit.bdp.dcs.desensitization.dto.DataMaskingPolicyDto;
import avicit.bdp.dcs.desensitization.service.DataMaskingMethodService;
import avicit.bdp.dcs.desensitization.service.DataMaskingPolicyService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2021-08-02
 * @类说明：DataMaskingRest
 * @修改记录：
 * @注意事项：
 * @主要功能：数据脱敏管理Rest接口
 */
@RestController
@Api(tags = "dcs", description = "数据脱敏管理Rest接口")
@RequestMapping("/api/bdp/dcs/dataMasking")
public class DataMaskingRest {
    /**
     * 数据脱敏方法服务
     */
    @Autowired
    private DataMaskingMethodService dataMaskingMethodService;

    /**
     * 数据脱敏策略服务
     */
    @Autowired
    private DataMaskingPolicyService dataMaskingPolicyService;

    /**
     * 新建脱敏策略
     * @param dataMaskingPolicyDto
     * @return
     */
    @PostMapping("/createDataMaskingPolicy")
    @ApiOperation(value = "新建脱敏策略")
    public ResponseMsg createDataMaskingPolicy(@ApiParam(value = "DataMaskingPolicyDto", name = "DataMaskingPolicyDto")
                                       @RequestBody DataMaskingPolicyDto dataMaskingPolicyDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.dataMaskingPolicyService.createDataMaskingPolicy(dataMaskingPolicyDto);

        return responseMsg;
    }

    /**
     * 修改脱敏策略
     * @param dataMaskingPolicyDto
     * @return
     */
    @PostMapping("/updateDataMaskingPolicy")
    @ApiOperation(value = "移除业务领域分类权限")
    public ResponseMsg updateDataMaskingPolicy(@ApiParam(value = "DataMaskingPolicyDto", name = "DataMaskingPolicyDto")
                                          @RequestBody DataMaskingPolicyDto dataMaskingPolicyDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.dataMaskingPolicyService.updateDataMaskingPolicy(dataMaskingPolicyDto);

        return responseMsg;
    }

    /**
     * 移除脱敏策略；多个id使用英文逗号隔开
     * @param dataMaskingPolicyDto
     * @return
     */
    @DeleteMapping("/deleteDataMaskingPolicy")
    @ApiOperation(value = "移除脱敏策略")
    public ResponseMsg deleteDataMaskingPolicy(@ApiParam(value = "DataMaskingPolicyDto", name = "DataMaskingPolicyDto")
                                  @RequestBody DataMaskingPolicyDto dataMaskingPolicyDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.dataMaskingPolicyService.deleteDataMaskingPolicy(dataMaskingPolicyDto);

        return responseMsg;
    }

    /**
     * 列举脱敏策略
     * @param sort
     * @param pageNo
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/listDataMaskingPolicy")
    @ApiOperation(value = "列举脱敏策略")
    public ResponseMsg<QueryRespBean<DataMaskingPolicyDto>> listDataMaskingPolicy(
            @ApiParam(value = "排序，按创建时间倒序：1，按创建时间升序：2，按名称升序：3，按名称倒序：4", name = "sort")
            @RequestParam(defaultValue = "1", required = false) Integer sort,
            @ApiParam(value = "当前页", name = "pageNo", required = true)
            @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @ApiParam(value = "每页显示条数", name = "pageSize", required = true)
            @RequestParam(defaultValue = "10", required = false) Integer pageSize,
            @ApiParam(value = "名称", name = "name") @RequestParam(required = false) String name) {
        ResponseMsg<QueryRespBean<DataMaskingPolicyDto>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dataMaskingPolicyService.listDataMaskingPolicy(sort, pageNo, pageSize, name));

        return responseMsg;
    }

    /**
     * 查询脱敏策略详情
     * @return
     */
    @GetMapping("/queryDataMaskingPolicyById")
    @ApiOperation(value = "查询脱敏策略详情")
    public ResponseMsg<DataMaskingPolicyDto> queryDataMaskingPolicyById(
            @ApiParam(value = "主键ID", name = "id", required = true)
            @RequestParam() String id) {
        ResponseMsg<DataMaskingPolicyDto> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dataMaskingPolicyService.queryDataMaskingPolicyById(id));

        return responseMsg;
    }

    /**
     * 获取脱敏算法类型
     * @return
     */
    @GetMapping("/getAllAlgoType")
    @ApiOperation(value = "查询脱敏算法类型")
    public ResponseMsg<List<String>> getAllAlgoType() {
        ResponseMsg<List<String>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dataMaskingMethodService.getAllAlgoType());

        return responseMsg;
    }

    /**
     * 列举权限列表
     * @param algoType 算法类型
     * @return
     */
    @GetMapping("/getMethodByAlgoType")
    @ApiOperation(value = "列举业务领域分类权限")
    public ResponseMsg<List<DataMaskingMethodDto>> getMethodByAlgoType(
            @ApiParam(value = "算法类型，哈希/掩码/变换/加密", name = "algoType")
            @RequestParam(defaultValue = "哈希") String algoType) {
        ResponseMsg<List<DataMaskingMethodDto>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dataMaskingMethodService.getMethodByAlgoType(algoType));

        return responseMsg;
    }
}


